import jieba

file = open("C:\\Users\\Administrator\\Desktop\\期中测试\\test.txt", "r", encoding="utf-8")
txt = file.read()

words = jieba.lcut(txt)
counts = {}
for s in words:
    if len(s) == 1:
        continue
    else:
        counts[s] = counts.get(s, 0) + 1
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
for i in range(5):
    word, count = items[i]
    print("{0:<5}->{1:>5}".format(word, count))
    f = open("output.txt", "a")
    f.write("{0:<5}->{1:>5}".format(word, count) + "\n")
f.close()
